package main

import (
	"fmt"
	"gitee.com/virtual-moon/vload"
)

// OrderDetail 订单详情
type OrderDetail struct {
	ID         int64   // 订单ID
	UserID     int64   // 用户ID
	UserName   string  // 用户名称
	GoodsID    int64   // 商品ID
	GoodsName  string  // 商品名称
	StoreID    int64   // 店铺ID
	StoreName  string  // 店铺名称
	Score      float64 // 评分
	ScoreDesc  string  // 评分描述
}

func main() {
	// 模拟数据
	orders := []OrderDetail{
		{ID: 1, UserID: 1001, GoodsID: 2001, StoreID: 3001, Score: 4.5},
		{ID: 2, UserID: 1002, GoodsID: 2002, StoreID: 3002, Score: 4.8},
		{ID: 3, UserID: 1003, GoodsID: 2001, StoreID: 3001, Score: 3.9},
	}

	// 创建加载器
	loader := vload.New()

	// 1. 添加用户信息映射
	userMap := map[int64]string{
		1001: "张三",
		1002: "李四",
		1003: "王五",
	}
	loader.AddMapping("users", []vload.Pair{
		{
			SourceField: "UserID",
			TargetField: "UserName",
		},
	}, func(ids []int64) (map[int64]string, error) {
		result := make(map[int64]string)
		for _, id := range ids {
			if name, ok := userMap[id]; ok {
				result[id] = name
			}
		}
		return result, nil
	})

	// 2. 添加商品信息映射
	goodsMap := map[int64]string{
		2001: "iPhone",
		2002: "MacBook",
	}
	loader.AddMapping("goods", []vload.Pair{
		{
			SourceField: "GoodsID",
			TargetField: "GoodsName",
		},
	}, func(ids []int64) (map[int64]string, error) {
		result := make(map[int64]string)
		for _, id := range ids {
			if name, ok := goodsMap[id]; ok {
				result[id] = name
			}
		}
		return result, nil
	})

	// 3. 添加店铺信息映射
	storeMap := map[int64]string{
		3001: "官方旗舰店",
		3002: "授权专卖店",
	}
	loader.AddMapping("stores", []vload.Pair{
		{
			SourceField: "StoreID",
			TargetField: "StoreName",
		},
	}, func(ids []int64) (map[int64]string, error) {
		result := make(map[int64]string)
		for _, id := range ids {
			if name, ok := storeMap[id]; ok {
				result[id] = name
			}
		}
		return result, nil
	})

	// 4. 添加评分描述映射
	loader.AddMapping("scores", []vload.Pair{
		{
			SourceField: "Score",
			TargetField: "ScoreDesc",
		},
	}, func(scores []float64) (map[float64]string, error) {
		result := make(map[float64]string)
		for _, score := range scores {
			switch {
			case score >= 4.5:
				result[score] = "优秀"
			case score >= 4.0:
				result[score] = "良好"
			default:
				result[score] = "一般"
			}
		}
		return result, nil
	})

	// 第一次使用：加载前两个订单的信息
	firstTwo := orders[:2]
	err := loader.Load(&firstTwo)
	if err != nil {
		fmt.Printf("加载订单1和2信息失败: %v\n", err)
		return
	}

	// 打印前两个订单的结果
	fmt.Println("=== 前两个订单的信息 ===")
	for _, order := range firstTwo {
		fmt.Printf("订单ID: %d, 用户: %s, 商品: %s, 店铺: %s, 评分: %s\n",
			order.ID, order.UserName, order.GoodsName, order.StoreName, order.ScoreDesc)
	}

	// 第二次使用：加载第三个订单的信息
	lastOne := orders[2:]
	err = loader.Load(&lastOne)
	if err != nil {
		fmt.Printf("加载订单3信息失败: %v\n", err)
		return
	}

	// 打印第三个订单的结果
	fmt.Println("\n=== 第三个订单的信息 ===")
	fmt.Printf("订单ID: %d, 用户: %s, 商品: %s, 店铺: %s, 评分: %s\n",
		lastOne[0].ID, lastOne[0].UserName, lastOne[0].GoodsName, lastOne[0].StoreName, lastOne[0].ScoreDesc)
}
